﻿using System;
using System.Net.Sockets;
using System.Text;

namespace RxjhServer.bbg
{
	public class ClientConnection : SockClienT
	{
		public ClientConnection(Socket sock, RemoveClientDelegateE rcd) : base(sock, rcd)
		{
		}

		public override void ProcessDataReceived(byte[] data, int length)
		{
			int num = 0;
			if (170 == data[0] && 102 == data[1])
			{
				byte[] array = new byte[4];
				Buffer.BlockCopy(data, 2, array, 0, 4);
				int num2 = BitConverter.ToInt32(array, 0);
				if (length < num2 + 6)
				{
					return;
				}
				while (true)
				{
					if (World.jlMsg == 1)
					{
						Form1.WriteLine(0, "ProcessDataReceived");
					}
					byte[] array2 = new byte[num2];
					Buffer.BlockCopy(data, num + 6, array2, 0, num2);
					num += num2 + 6;
					this.DataReceived(array2, num2);
					if (num >= length)
					{
						break;
					}
					if (data[num] != 170)
					{
						return;
					}
					if (data[num + 1] != 136)
					{
						return;
					}
					Buffer.BlockCopy(data, num + 2, array, 0, 4);
					num2 = (int)BitConverter.ToInt16(array, 0);
				}
				return;
			}
			else
			{
				Form1.WriteLine(1, string.Concat(new object[]
				{
					"错包1：",
					data[0],
					" ",
					data[1]
				}));
			}
		}

        public void DataReceived(byte[] data, int length)
        {
            if (World.jlMsg == 1)
            {
                Form1.WriteLine(0, "ProcessDataReceived()");
            }
            try
            {
                byte[] bytes = new byte[length];
                for (int index = 0; index < length; ++index)
                {
                    bytes[index] = data[index];
                }
                string str1 = Encoding.Default.GetString(bytes);
                string str2 = "-1";
                string[] strArray = str1.Split(',');
                // Form1.WriteLine(2, strArray[0]);
                switch (strArray[0])
                {



                    case "用户登陆超变态jc": //七夕=1用户登陆1  用户登陆超变态jc,账号,10,10,ewfwef,0,26
                        {
                            Players players1 = World.检查玩家(strArray[1]);
                            str2 = ((players1 != null) ? "登陆成功" : "登陆失败");
                            //  Form1.WriteLine(2, "百宝登陆|"+ strArray[1]+"|"+ str2);
                            break;
                        }
                    case "查询超变百宝":
                        {
                            Players players1 = World.检查玩家(strArray[1]);
                            if (players1 == null)
                            {
                                str2 = "-1";
                                // Form1.WriteLine(2, "百宝登陆|不在线|-1");
                            }
                            else if (strArray[2] == "热血元宝")
                            {
                                players1.查百宝阁元宝数();
                                str2 = players1.FLD_RXPIONT.ToString();
                                //   Form1.WriteLine(2, "百宝元宝|" + strArray[1] + "|" + str2);
                            }
                            else if (strArray[2] == "赠品元宝")
                            {
                                players1.查百宝阁元宝数();

                                str2 = players1.FLD_积分.ToString();
                                //  Form1.WriteLine(2, "百宝积分|" + strArray[1] + "|" + str2);
                            }
                            else if (strArray[2] == "空位")
                            {
                                str2 = players1.得到包裹空位数().ToString();
                                Form1.WriteLine(2, "百宝人物背包空位|" + strArray[1] + "|" + str2);
                            }
                            else
                            {
                                Form1.WriteLine(2, "百宝登陆|未知错误1|" + strArray[2]);

                            }

                            break;

                        }





                    case "购买超变百宝"://购买超变百宝,2,16900120,1,12,0,  0=元宝  1=积分  
                        {

                            Players players2 = World.检查玩家(strArray[1]);
                            if (players2 == null)
                            {
                                str2 = "-1";
                                Form1.WriteLine(1, "百宝购买错误|账号" + strArray[1]);
                                break;
                            }
                            if (strArray[5] == "5")
                            {
                                if (World.寄售系统是否开启 == 0)
                                {
                                    players2.系统提示("当前线路不支持寄售，请到银币广场购买！", 10, "寄售");
                                    str2 = "-1";
                                    break;
                                }




                                str2 = players2 != null ? (int.Parse(strArray[5]) != 5 ? players2.百宝阁寄售买卖(int.Parse(strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4])) : players2.百宝阁寄售买卖(int.Parse(strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4]))) : "-1";
                                Form1.WriteLine(2, "百宝寄售购买|账号" + strArray[1] + "|D" + strArray[2] + "|数量" + strArray[3] + "|元宝" + strArray[4]);
                            }
                            else
                            {

                                str2 = players2 != null ? (int.Parse(strArray[5]) != 5 ? players2.百宝阁买卖东西(int.Parse(strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4])) : players2.百宝阁买卖东西(int.Parse(strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4]))) : "-1";
                                Form1.WriteLine(2, "百宝购买|账号" + strArray[1] + "|物品ID" + strArray[2] + "|数量" + strArray[3] + "|元宝" + strArray[4]);
                            }
                            break;
                        }




                    case "4领取元宝4":
                        if (World.检查玩家(strArray[1]) == null)
                        {
                            str2 = "-1";
                            Form1.WriteLine(1, "百宝对接|账号" + strArray[1] + "非法的请求|4领取元宝4");
                            break;
                        }
                        break;
                    case "5删除寄售5"://七夕原端口
                        Players players3 = World.检查玩家(strArray[1]);
                        str2 = "-1";
                        //str2 = players3 != null ? players3.百宝阁取消寄售(int.Parse(strArray[2]), int.Parse(strArray[3])) : "-1";
                        break;
                    default:
                        str2 = "-1";
                        break;
                }
                this.Sendd1(str2);
            }
            catch (Exception ex)
            {
                this.Dispose();
            }
        }
    }
}
